//
//  PortraitAnimationController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/5.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "PotraitAnimationController.h"
#import "PortraitViewController.h"
#import "PortraitDetailViewController.h"

@interface PotraitAnimationController ()
@property (strong, nonatomic) UIView *dimmingView;
@end

@implementation PotraitAnimationController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dimmingView = [[UIView alloc] init];
        self.dimmingView.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return .6f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    UIViewController *destVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *sourceVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    
    if (destVc.isBeingPresented) {
    
        PortraitDetailViewController *toVc = (PortraitDetailViewController *)destVc;
        
        [containerView addSubview:toVc.view];
        
        CGRect toVcFinalRect = [transitionContext finalFrameForViewController:toVc];
        toVc.view.frame = CGRectOffset(toVcFinalRect, 0, CGRectGetHeight(toVcFinalRect));
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:.8 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            toVc.view.frame = toVcFinalRect;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
        
    }
    
    if (sourceVc.isBeingDismissed) {
        
        PortraitDetailViewController *fromVc = (PortraitDetailViewController *)sourceVc;
        
        // presentStyle == Custom时，不要将toVc的view加到containerView上
//        UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//        UIView *containerView = [transitionContext containerView];
//        [containerView addSubview:toVc.view];±
        
        CGRect initialRect = [transitionContext initialFrameForViewController:fromVc];
        CGRect fromViewFinalRect = CGRectOffset(initialRect, 0, CGRectGetHeight(initialRect));
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:.5 initialSpringVelocity:.8 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveLinear animations:^{
            fromVc.view.frame = fromViewFinalRect;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    
}


- (void)dealloc {
    
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}
@end
